<?php if (!defined("BASEPATH")) exit("No direct script access allowed");

class Tennis extends MY_Controller {

	private $insertedOdds = 0;
	private $sportId = 2;

	function __construct() {
		parent::__construct();
	}
	
	function index() {
		$this->odds_model->clearOdds();
		foreach($this->bookie_model->getBookie() as $bookie) {
			$getHtmlByBookie = $this->html_model->getHtmlByBookie($bookie["bookie_name"], $this->sportId);
			if(!count($getHtmlByBookie)) continue;
			foreach($getHtmlByBookie as $htmlData) {
				$html = str_get_html($htmlData["html_text"]);
				$this->insertedOdds = 0;
				switch($bookie["bookie_name"]) {
				
					case "Bwin": {
						//markets-container
						$markets_container = $html->find("div[id=markets-container]", 0);
						//var_dump($markets_container);
						foreach($markets_container->find("table[class=options-wide]") as $options_wide) {
							$insUpd = array();
							$insUpd["odds_html_id"] = $htmlData["html_id"];
							$insUpd["odds_team1"] = Common::prepare_str($options_wide->find("span[class=option-name]", 0)->plaintext);
							$insUpd["odds_team2"] = Common::prepare_str($options_wide->find("span[class=option-name]", 1)->plaintext);
							$insUpd["odds_odds1"] = Common::prepare_str($options_wide->find("span[class=odds]", 0)->plaintext);
							$insUpd["odds_odds2"] = NULL;
							$insUpd["odds_odds3"] = Common::prepare_str($options_wide->find("span[class=odds]", 1)->plaintext);
							//Common::pr($insUpd);
							$this->odds_model->insert($insUpd);
							$this->insertedOdds++;
						}
					} break;
					
					case "Sportingbet": {
						foreach($html->find("div[class=columns]") as $column) {
							if($column->find("div[class=eventInfo]", 0)) {
								$eventName = preg_split("/-/", $column->find("div[class=eventName]", 0)->innertext);
								$insUpd = array();
								$insUpd["odds_html_id"] = $htmlData["html_id"];
								$insUpd["odds_team1"] = Common::prepare_str($eventName[0]);
								$insUpd["odds_team2"] = Common::prepare_str($eventName[1]);
								$insUpd["odds_odds1"] = Common::prepare_str($column->find("span[class=priceText wide  EU]",0)->plaintext);
								$insUpd["odds_odds2"] = NULL;
								$insUpd["odds_odds3"] = Common::prepare_str($column->find("span[class=priceText wide  EU]",1)->plaintext);
								//Common::pr($insUpd);
								$this->odds_model->insert($insUpd);
								$this->insertedOdds++;
							}
						}
					} break;
					
					case "Betfair": {
						$main = $html->find("div[id=main]", 0)->children(1);
						foreach($main->find("table[class=reduced-spacing-table-left]") as $table) {
							if($table->find("span[class=home-team]", 0)) {
								$insUpd = array();
								$insUpd["odds_html_id"] = $htmlData["html_id"];
								$insUpd["odds_team1"] = Common::prepare_str($table->find("span[class=home-team]", 0)->plaintext);
								$insUpd["odds_team2"] = Common::prepare_str($table->find("span[class=away-team]", 0)->plaintext);
								$insUpd["odds_odds1"] = Common::prepare_str($table->next_sibling()->find("span[class=price]", 0)->plaintext);
								$insUpd["odds_odds1_back"] = Common::prepare_str($table->next_sibling()->find("span[class=price]", 1)->plaintext);
								$insUpd["odds_odds2"] = NULL;
								$insUpd["odds_odds3"] = Common::prepare_str($table->next_sibling()->find("span[class=price]", 2)->plaintext);
								$insUpd["odds_odds3_back"] = Common::prepare_str($table->next_sibling()->find("span[class=price]", 3)->plaintext);
								//Common::pr($insUpd);
								$this->odds_model->insert($insUpd);
								$this->insertedOdds++;
							}
						}
					} break;
					
					case "Bet365": {
						$lines = $html->find("div[class=Line]");
						foreach($lines as $ii => $line) {
							if($ii % 2 == 0) {
								$line = preg_split("/ /", $line->plaintext);
								$linepair = preg_split("/ /", $lines[$ii+1]->plaintext);
								$insUpd = array();
								$insUpd["odds_html_id"] = $htmlData["html_id"];
								eval("\$insUpd[\"odds_odds1\"] = 1+".$line[0].";");
								unset($line[0]);
								$insUpd["odds_odds2"] = NULL;
								eval("\$insUpd[\"odds_odds3\"] = 1+".$linepair[0].";");
								unset($linepair[0]);
								$insUpd["odds_team1"] = Common::prepare_str(implode(" ", $line));
								$insUpd["odds_team2"] = Common::prepare_str(implode(" ", $linepair));
								//Common::pr($insUpd);
								$this->odds_model->insert($insUpd);
								$this->insertedOdds++;
							}
						}
					} break;
					
					case "Betclick": {
						$matches = $html->find("div[class=match-entry clearfix]");
						foreach($matches as $ii => $match) {
							$insUpd = array();
							$match_name = preg_split("/ - /", $match->find("div[class=match-name]", 0)->plaintext);
							$insUpd["odds_html_id"] = $htmlData["html_id"];
							$insUpd["odds_team1"] = Common::prepare_str($match_name[0]);
							$insUpd["odds_team2"] = Common::prepare_str($match_name[1]);
							$insUpd["odds_odds1"] = Common::prepare_str($match->find("span[class=odd-button]", 0)->plaintext);
							$insUpd["odds_odds2"] = NULL;
							$insUpd["odds_odds3"] = Common::prepare_str($match->find("span[class=odd-button]", 1)->plaintext);
							//Common::pr($insUpd);
							$this->odds_model->insert($insUpd);
							$this->insertedOdds++;
						}
					} break;
					
					case "Williamhill": {
						$rowOdds = $html->find("tr[class=rowOdd]");
						foreach($rowOdds as $ii => $rowOdd) {
							$insUpd = array();
							$match_name = preg_split("/ v /", $rowOdd->find("td[class=CentrePad]", 0)->plaintext);
							$insUpd["odds_html_id"] = $htmlData["html_id"];
							$insUpd["odds_team1"] = Common::prepare_str($match_name[0]);
							$insUpd["odds_team2"] = Common::prepare_str($match_name[1]);
							$insUpd["odds_odds1"] = Common::prepare_str($rowOdd->find("div[class=eventprice]", 0)->plaintext);
							$insUpd["odds_odds2"] = NULL;
							$insUpd["odds_odds3"] = Common::prepare_str($rowOdd->find("div[class=eventprice]", 1)->plaintext);
							$this->odds_model->insert($insUpd);
							$this->insertedOdds++;
						}
					} break;
					
					case "expekt": {
						foreach($html->find("div[class=cal-day-entry]") as $calDayEntry) {
							$odds_datetime = substr($calDayEntry->getAttribute("data-date"), 0, 10);
							
							foreach($calDayEntry->find("div[class=cal-schedule]") as $calSchedule) {
								if(count($calSchedule->find("p[class=event-name]"))) {
									$odds_league = $calSchedule->find("p[class=event-name]", 0)->plaintext;
								}
								$events = $calSchedule->find("div[class=event]");
								
								foreach($events as $ii => $event) {
									$insUpd = array();
									$insUpd["odds_type"] = "TENNIS";
									$insUpd["odds_league"] = $odds_league;
									$insUpd["odds_date"] = $insUpd["odds_datetime"] = $odds_datetime;
									if($event->find("div[class=cal-hour]", 0)) {
										$insUpd["odds_datetime"] .= " ".Common::prepare_str($event->find("div[class=cal-hour]", 0)->plaintext);
									}
									$match_name = preg_split("/ - /", $event->find("div[class=match-name]", 0)->plaintext);
									$insUpd["odds_team1"] = Common::prepare_str($match_name[0]);
									$insUpd["odds_team2"] = Common::prepare_str($match_name[1]);
									$insUpd["odds_type"] = $htmlData["html_type"];
									$insUpd["odds_html_id"] = $htmlData["html_id"];
									$insUpd["odds_odds1"] = Common::prepare_str($event->find("span[class=oddValue]", 0)->plaintext);
									$insUpd["odds_odds2"] = NULL;
									$insUpd["odds_odds3"] = Common::prepare_str($event->find("span[class=oddValue]", 1)->plaintext);
									//Common::pr($insUpd);
									$this->odds_model->insert($insUpd);
									$this->insertedOdds++;
								}
							}
						}
					} break;
					
					case "SkyBet": {
						$trs = $html->find("tr");
						foreach($trs as $ii => $tr) {
							if(count($tr->find("td[class=outcome]"))) {
								$insUpd = array();
								$insUpd["odds_html_id"] = $htmlData["html_id"];
								$insUpd["odds_team1"] = Common::prepare_str($tr->find("span[class=oc-desc]", 0)->plaintext);
								$insUpd["odds_team2"] = Common::prepare_str($tr->find("span[class=oc-desc]", 1)->plaintext);
								$odds1 = Common::prepare_str($tr->find("b[class=odds]", 0)->plaintext);
								eval("\$insUpd[\"odds_odds1\"] = 1+".$odds1.";");
								$insUpd["odds_odds2"] = NULL;
								$odds3 = Common::prepare_str($tr->find("b[class=odds]", 1)->plaintext);
								eval("\$insUpd[\"odds_odds3\"] = 1+".$odds3.";");
								//Common::pr($insUpd);
								$this->odds_model->insert($insUpd);
								$this->insertedOdds++;
							}
						}
					} break;
					
					case "betway": {
						$odds_browsers = $html->find("table[class=odds-browser]");
						foreach($odds_browsers as $ii => $odds_browser) {
							foreach($odds_browser->find("tr") as $tr) {
								if(count($tr->find("td[class=time]"))) {
									$teams = preg_split("/ - /", $tr->find("td", 1)->plaintext);
									$insUpd = array();
									$insUpd["odds_html_id"] = $htmlData["html_id"];
									$insUpd["odds_team1"] = Common::prepare_str($teams[0]);
									$insUpd["odds_team2"] = Common::prepare_str($teams[1]);
									$insUpd["odds_odds1"] = Common::prepare_str($tr->find("a[class=outcome]", 0)->plaintext);
									$insUpd["odds_odds2"] = NULL;
									$insUpd["odds_odds3"] = Common::prepare_str($tr->find("a[class=outcome]", 1)->plaintext);
									//Common::pr($insUpd);
									$this->odds_model->insert($insUpd);
									$this->insertedOdds++;
								}
							}
						}
					} break;
					
					
					case "bet-at-home": {
						$tables = $html->find("table[class=ods s-expanded]");
						foreach($tables as $ii => $table) {
							$league = $table->parent()->prev_sibling()->find("h1[class=hea-text]")[0]->plaintext;
							foreach($table->find("tr") as $jj => $tr) {
								if($jj and count($tr->find("td[ods-tbody-td ods-odd]") == 2)) {
									$teams = preg_split("/ - /", $tr->find("td", 0)->plaintext);
									$insUpd = array();
									$insUpd["odds_html_id"] = $htmlData["html_id"];
									$insUpd["odds_league"] = $league;
									$insUpd["odds_type"] = "TENNIS";
									$date = Common::prepare_str($tr->find("td[class=date]", 0)->plaintext);
									$odds_date = "20".substr($date, 6, 2).".".substr($date, 3, 2).".".substr($date, 0, 2);
									$insUpd["odds_date"] = $odds_date;
									$insUpd["odds_datetime"] = $odds_date." ".substr($date, 9);
									$insUpd["odds_team1"] = Common::prepare_str($teams[0]);
									$insUpd["odds_team2"] = Common::prepare_str($teams[1]);
									$insUpd["odds_odds1"] = Common::prepare_str($tr->find("td[class=ods-tbody-td ods-odd]", 0)->plaintext);
									$insUpd["odds_odds2"] = NULL;
									$insUpd["odds_odds3"] = Common::prepare_str($tr->find("td[class=ods-tbody-td ods-odd]", 1)->plaintext);
									//Common::pr($insUpd);
									$this->odds_model->insert($insUpd);
									$this->insertedOdds++;
								}
							}
						}
					} break;
					
					case "youwin": {
						$tables = $html->find("table[class=eventT]");
						foreach($tables as $ii => $table) {
							if(count($table->find("td")) == 4) {
								$insUpd = array();
								$insUpd["odds_html_id"] = $htmlData["html_id"];
								$insUpd["odds_team1"] = Common::prepare_str($table->find("td", 0)->plaintext);
								$insUpd["odds_team2"] = Common::prepare_str($table->find("td", 2)->plaintext);
								$insUpd["odds_odds1"] = Common::prepare_str($table->find("span[class=odd]", 0)->plaintext);
								$insUpd["odds_odds2"] = NULL;
								$insUpd["odds_odds3"] = Common::prepare_str($table->find("span[class=odd]", 1)->plaintext);
								//Common::pr($insUpd);
								$this->odds_model->insert($insUpd);
								$this->insertedOdds++;
							}
						}
					} break;
					
					case "betonline": {
						$trs = $html->find("tr[class=otherline]");
						foreach($trs as $ii => $tr) {
							$insUpd = array();
							$insUpd["odds_html_id"] = $htmlData["html_id"];
							$insUpd["odds_team1"] = Common::prepare_str($tr->prev_sibling()->find("td", 2)->plaintext);
							$insUpd["odds_team2"] = Common::prepare_str($tr->find("td", 1)->plaintext);
							$insUpd["odds_odds1"] = Common::american2decimal((int)Common::prepare_str($tr->prev_sibling()->find("td[class=moneylineodds]", 0)->plaintext));
							$insUpd["odds_odds2"] = NULL;
							$insUpd["odds_odds3"] = Common::american2decimal((int)Common::prepare_str($tr->find("td[class=moneylineodds]", 0)->plaintext));
							//Common::pr($insUpd);
							$this->odds_model->insert($insUpd);
							$this->insertedOdds++;
						}
					} break;
					
					case "NetBet": {
						$bets = $html->find("div[class=bets]");
						foreach($bets as $ii => $bet) {
							if($bet->find("dt", 0)->plaintext) {
								$insUpd = array();
								$insUpd["odds_html_id"] = $htmlData["html_id"];
								$teams = preg_split("/ vs /", $bet->find("dt", 0)->plaintext);
								if(count($teams) == 2) {
									$insUpd["odds_team1"] = Common::prepare_str($teams[0]);
									$insUpd["odds_team2"] = Common::prepare_str($teams[1]);
									$insUpd["odds_odds1"] = Common::prepare_str($bet->find("li[class=first]", 0)->plaintext);
									$insUpd["odds_odds2"] = NULL;
									$insUpd["odds_odds3"] = Common::prepare_str($bet->find("li[class=first]", 0)->next_sibling()->plaintext);
									//Common::pr($insUpd);
									$this->odds_model->insert($insUpd);
									$this->insertedOdds++;
								}
							}
						}
					} break;
					
					case "Pinnacle": {
						$tables =  $html->find("table[class=linesTbl]");
						foreach($tables as $table) {
							if(trim($table->find("tr[class=linesColumns]", 0)->find("td", 0)->plaintext) == "Match") {
								$trs = $table->find("tr[class=linesAlt1]");
								foreach($trs as $ii => $tr) {
									if($ii % 2 == 0 and $tr->find("td[class=linesTeam]",0) and $tr->next_sibling()) {
										$insUpd["odds_html_id"] = $htmlData["html_id"];
										$insUpd["odds_team1"] = Common::prepare_str($tr->find("td[class=linesTeam]",0)->plaintext);
										$insUpd["odds_team2"] = Common::prepare_str($tr->next_sibling()->find("td[class=linesTeam]",0)->plaintext);
										$insUpd["odds_odds1"] = (float)Common::prepare_str(trim($tr->find("td[class=linesMLine]",0)->plaintext));
										$insUpd["odds_odds2"] = NULL;
										$insUpd["odds_odds3"] = (float)Common::prepare_str(trim($tr->next_sibling()->find("td[class=linesMLine]",0)->plaintext));
										//Common::pr($insUpd);
										if($insUpd["odds_odds1"] and $insUpd["odds_odds3"] and strpos($insUpd["odds_team1"], "(-1.5 Sets)") === FALSE and strpos($insUpd["odds_team1"], "(+1.5 Sets)") === FALSE) {
											$this->odds_model->insert($insUpd);
											$this->insertedOdds++;
										}
									}
								}
							}
						}
					} break;
					
					case "PaddyPower": {
						$tds =  $html->find("td[class=bets]");
						foreach($tds as $td) {
							$match = preg_split("/ v /", $td->parent()->find("td[class=mkt-td-label]", 0)->plaintext);
							$insUpd = array();
							$insUpd["odds_html_id"] = $htmlData["html_id"];
							$insUpd["odds_team1"] = $match[0];
							$insUpd["odds_team2"] = $match[1];
							if(preg_match("/[0-9]*\/[0-9]*/", $td->parent()->find("div[class=odds]",0)->plaintext) and preg_match("/[0-9]*\/[0-9]*/", $td->parent()->find("div[class=odds]",1)->plaintext)) {
								eval("\$insUpd[\"odds_odds1\"] = (".$td->parent()->find("div[class=odds]",0)->plaintext.");");
								$insUpd["odds_odds2"] = NULL;
								eval("\$insUpd[\"odds_odds3\"] = (".$td->parent()->find("div[class=odds]",1)->plaintext.");");
								//Common::pr($insUpd);
								$this->odds_model->insert($insUpd);
								$this->insertedOdds++;
							}
						}
					} break;
					
				}
				
				
				$html->clear(); 
				unset($html);
				// html update
				$this->html_model->update($htmlData["html_id"], array(
					"html_parsed_time" => Common::now(),
					"html_inserted_odds" => $this->insertedOdds,
				));
			}
		}
		
		// odds fix/mapping
		//$this->odds_model->tableFix();
		$this->odds_model->mapping();
	}

}